package {
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.FP;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;

	public class Player extends PhysicsEntity {
		[Embed(source = 'assets/mario.png')]
		private const MARIO : Class;
		public var mario : Spritemap = new Spritemap(MARIO, 19, 28);
		private var rock : Rock = null;
		private var maxRadius : Number = 100;
		private var throwPower : Number = 0.15;
		private var power : Number = 0.5;
		private var jumpPower : Number = 10;
		private var hFriction : Number = 0.95;
		private var launchDelay : Boolean = false;

		public function Player(x : Number, y : Number) {
			// Here I set the hitbox width/height with the setHitbox function.
			setHitbox(19, 28);
			width = 19;
			height = 28;
			mario.add("stand_right", [0], 1, true);
			mario.add("stand_left", [1], 1, true);
			mario.add("run_right", [4, 5], 5, true);
			mario.add("run_left", [8, 9], 5, true);
			graphic = mario;

			this.x = x;
			this.y = y;

			// Here I do the same thing by just assigning Player's properties.
			mario.play("stand_right");
		}

		override public function update() : void {
			super.update();
			xSpeed *= hFriction;

			var pressed : Boolean = false;
			if (Input.check(Key.A)) {
				xSpeed -= power;
				pressed = true;
			}
			if (Input.check(Key.D)) {
				xSpeed += power;
				pressed = true;
			}
			if (Math.abs(xSpeed) < 0.3 && !pressed) {
				xSpeed = 0;
			}
			if (onTheGround) {
				if (Input.check(Key.W)) {
					ySpeed -= jumpPower;
				}
			}
			if (rock) {
				if (Input.mousePressed) {
					rock.setSpeed((rock.x - this.x) * throwPower, (rock.y - this.y) * throwPower);
					rock = null;
					launchDelay = true;
				} else {
					var x : int = FP.world.mouseX;
					var y : int = FP.world.mouseY;
					var xVector : int = x - this.x;
					var yVector : int = y - this.y;

					var hyp : int = Math.sqrt(Math.pow(xVector, 2) + Math.pow(yVector, 2));
					if ( hyp > maxRadius) {
						var difference : Number = hyp / maxRadius;
						xVector /= difference;
						yVector /= difference;
					}
					rock.moveTo(this.x + xVector - rock.halfWidth, this.y + yVector - rock.halfHeight);
				}
			}
			if (!rock) {
				if (launchDelay) {
					if (Input.mouseReleased)
						launchDelay = false;
				}
				if (!launchDelay) {
					if (Input.mouseDown) {
						var xVector : int = FP.world.mouseX - this.x;
						var yVector : int = FP.world.mouseY - this.y;
						var hyp : int = Math.sqrt(Math.pow(xVector, 2) + Math.pow(yVector, 2));
						if (hyp < maxRadius) {
							rock = collide("bullet", FP.world.mouseX, FP.world.mouseY) as Rock;
						}
					}
				}
			}

			if (Input.pressed(Key.A)) {
				mario.play("run_left");
			}

			if (Input.pressed(Key.D)) {
				mario.play("run_right");
			}

			if (Input.released(Key.A)) {
				mario.play("stand_left");
			}

			if (Input.released(Key.D)) {
				mario.play("stand_right");
			}

			if (Input.pressed(Key.SPACE)) {
				FP.world.add(new Bullet(x + 20, y));
			}
		}
	}
}
